<?php
namespace app\mobile\controller;

use app\model\Util; 
use app\model\Theme; 
use app\model\Poster; 
use app\model\Worker; 

class OldAddworker extends Base
{
    public function Old_addworker()
    {
global $_G, $_P;
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$system=$_G['system'];
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('活动入口不正确');
}
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
    die(Util::showError("请确认活动地址是否正确"));
    return;
}
$config = Theme::getConfig($themeid);

if ($op == 'save') {
	$myworker = Worker::getThemeWorkerByOpenidNew($_G['openid'],$themeid);
	$data = array (
		'uniacid' => $_G['mpid'],
		'uid'     => $_G['member']['uid'],
		'openid'  => $_G['openid'],
		'username'=> $_P['username'],
		'mobile'  => $_P['mobile'],
		'company'  => intval($_P['company']),
		'qrcode'	  => $_P['qr'],
		'status'  => 1,
		'type'    => 'theme',
	);
	$alltheme = Theme::getAllTheme();
	if (!empty($alltheme)) {
		foreach ($alltheme as $key => $item) {
			$data ['themeid'] = $item['id'];
			$myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$item['id'],1);
			if (empty($myworker)){
				$data ['addtime'] = TIMESTAMP;
				db_insert ( t_worker, $data );
			}else{
				db_update (t_worker, $data, array ('openid' => $_G['openid']));
			}
		}
	}
	db_query("UPDATE ".tablename(t_relation)." SET parentid = ".$_G['member']['uid'].",popenid='".$_G['openid']."',workerid = ".$_G['member']['uid']." WHERE openid ='".$_G['openid']."'");

	return_msg('员工信息保存成功','','success');
    return;
}
$myworker = Worker::getThemeWorkerByOpenidNew($_G['openid'],$themeid);

$shoplist = db_select ( "SELECT id,name FROM " . tablename (t_shangjia) .
				" WHERE  state = 1 order by displayorder" );

$sharedata = array(
    'themeid'=>$themeid,
    'fopenid'=>$_G['openid'],
    'fromuid'=>$_G['member']['uid'],
    'workerid'=>$myworker['uid'],
    'flag'=>1,
);
    if ($system['gongzhong'] =='1'){
    	$domaintxt = $system['domainhead1'];
    }else{
    	$domaintxt = DOMAIN0;
    }
	$workerurl = "https://".$domaintxt.'/app/'.createMobileUrl('index',$sharedata);

$workerurl = Poster::GetWorkerUrl($system,$workerurl,MODULE_NAME ."员工:".$myworker['username']);

$return_clicent = get_defined_vars();
return view('/yxpdd/addworker', $return_clicent);

}
}